package kr.hkit.powerjava.chapter26.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import kr.hkit.powerjava.chapter26.common.DBUtil;
import kr.hkit.powerjava.chapter26.dto.Employee;

public class EmployeeDAO implements DaoInterface<Employee>{
	private Connection connection;
	private PreparedStatement pstmt;
	
	public EmployeeDAO(Connection connection) {
		this.connection = connection;
	}
	
	@Override
	public void insertItem(Employee item) throws SQLException {
		String sql = "insert into employee values(?, ?, ?, ?, ?, ?)";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		pstmt.setString(3, item.getTitle());
		pstmt.setInt(4, item.getManager());
		pstmt.setInt(5, item.getSalary());
		pstmt.setInt(6, item.getDno());
		
		int res = pstmt.executeUpdate();
		DBUtil.close(pstmt);
	}

	@Override
	public void deleteItembyNo(Employee item) throws SQLException {
		String sql = "delete from employee where empno = ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		int res = pstmt.executeUpdate();
		DBUtil.close(pstmt);
	}

	@Override
	public void updateItembyNo(Employee item) throws SQLException {
		String sql = "update employee set empname = ?, title = ?, manager = ?, salary = ?, dno = ? where empno = ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getEmpName());
		pstmt.setString(2, item.getTitle());
		pstmt.setInt(3, item.getManager());
		pstmt.setInt(4, item.getSalary());
		pstmt.setInt(5, item.getDno());
		pstmt.setInt(6, item.getEmpNo());
		int res = pstmt.executeUpdate();
		DBUtil.close(pstmt);
		
	}

	@Override
	public ArrayList<Employee> listItems() throws SQLException {
		ArrayList<Employee> list = new ArrayList<>();
		String sql = "select empno, empname, title, manager, salary, dno from employee";
		pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) { //next() ->다음 레코드가 있느냐?? 있으면 true, 없으면 false
			list.add(new Employee(
					rs.getInt("empno"), 
					rs.getString("empname"), 
					rs.getString("title"),
					rs.getInt("manager"),
					rs.getInt("salary"),
					rs.getInt("dno")
					));
		}
		DBUtil.close(rs);
		DBUtil.close(pstmt);
		return list;
	}

	@Override
	public Employee searchItem(Employee item) throws SQLException {
		Employee employee = null;
		String sql = "select empno, empname, title, manager, salary, dno from employee where empno = ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()) {
			employee = new Employee();
			employee.setEmpNo(rs.getInt(1));
			employee.setEmpName(rs.getString(2));
			employee.setTitle(rs.getString(3));
			employee.setManager(rs.getInt(4));
			employee.setSalary(rs.getInt(5));
			employee.setDno(rs.getInt(6));
		} 
		
		return employee;
	}
	
}

